//
//  TaskAssignViewController.m
//  Conference
//
//  Created by Xiang He on 12-11-19.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "TaskAssignViewController.h"
#import "ActivitiesData.h"
#import "TaskAssignDetailViewController.h"
#import "ActivityArragnCellController.h"
#import "MainDataBase.h"
#import "NetContant.h"
#import "Globals.h"
#import "WebData.h"
#import "Constant.h"
#import "DateTool.h"
@interface TaskAssignViewController ()

@end

@implementation TaskAssignViewController
@synthesize table;
@synthesize arrayOfData;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self loadingData];
  
    // Do any additional setup after loading the view from its nib.
}
-(void)loadingData{
    self.arrayOfData=[NSMutableArray arrayWithCapacity:0];
    NSThread *thread=[[[NSThread alloc] initWithTarget:self selector:@selector(run) object:nil] autorelease];
    [thread start];
    
}

-(void)run{
    NSAutoreleasePool *pool=[[NSAutoreleasePool alloc] init];
    NSString *sRet=[[WebData getInstance] getActivities];;
    
    [self performSelectorOnMainThread:@selector(onPostExecute:) withObject:sRet waitUntilDone:YES];
    
	[pool release];
    
}

-(void)onPostExecute:(NSString *)sRet{
    
    if ([sRet isEqualToString:[[NetContant getInstance] NET_CONNECT_FALSE]]) {
        
        
        //[[Globals getInstance] showAlertView:[[Constant getInstance] net_error]];
        
        
    }else if([sRet isEqualToString:[[NetContant getInstance] NETWORK_RECORD_ISEMPTY]]){
        
        //[[Globals getInstance] showAlertView:[[Constant getInstance] account_error]];
        
    }else {
        @try {
            [self doNetRet:sRet];
        }
        @catch (NSException *exception) {
            
        }
        
        
    }
    
}
-(void)doNetRet:(NSString *)sRet{
     NSArray *tables=[sRet componentsSeparatedByString:[[NetContant getInstance]TABLES_SPLIT_FLAG]];
    [self.arrayOfData removeAllObjects]; 
    for (int i=0; i<[tables count]; i++) {
               if(nil!=[tables objectAtIndex:i]){
                   NSArray *record=[[tables objectAtIndex:i] componentsSeparatedByString:[[NetContant getInstance] RECORDS_SPLIT_FLAG]];;
                  if([record count]>=5){
                       ActivitiesData *activitiesData=[[ActivitiesData alloc]init];
                       activitiesData.ID=[record objectAtIndex:0];
                        activitiesData.title = [record objectAtIndex:1];
                        activitiesData.message = [record objectAtIndex:2];
                        activitiesData.urlPhoto=[record objectAtIndex:3];
                        activitiesData.dateTime=[record objectAtIndex:4];

                        [self.arrayOfData addObject:activitiesData];
                        [activitiesData release];
                    }
               }
          }
     [self.table reloadData];
}
- (void)viewDidUnload
{
    [self setArrayOfData:nil];
    [self setTable:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
    return cell.frame.size.height;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    //    return 0;
    return [self.arrayOfData count];
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *activityCell=@"activityCell";
    ActivityArragnCellController *cell=(ActivityArragnCellController *)[tableView dequeueReusableCellWithIdentifier:activityCell];
    if(cell ==nil){
		NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"ActivityArragnCellController" owner:self options:nil];
		for (id oneObject in array) {
			if ([oneObject isKindOfClass:[ActivityArragnCellController class]]) {
				cell=oneObject;
				break;
			}
		}
        cell.selectionStyle=UITableViewCellSelectionStyleBlue;
    }
    ActivitiesData  *data=[self.arrayOfData objectAtIndex:[indexPath row]];
    cell.lab_content.text=data.title;
    cell.lab_update_time.text=data.dateTime;
    return cell;
    
}

-(void)tableView:(UITableView *)tableView1 didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    TaskAssignDetailViewController *mTaskAssignDetailViewController=[[TaskAssignDetailViewController alloc]initWithNibName:@"TaskAssignDetailViewController" bundle:nil];
    mTaskAssignDetailViewController.activitiesData=[self.arrayOfData objectAtIndex:[indexPath row]];
    [self.navigationController pushViewController:mTaskAssignDetailViewController animated:YES];
    [mTaskAssignDetailViewController release];
     [tableView1 deselectRowAtIndexPath:indexPath animated:YES];
    
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)dealloc {
    [arrayOfData release];
    [table release];
    [super dealloc];
}
@end
